package fact.view.util;

import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

public class MyApplicationContext {

	private static transient ServletContext sc = null;
	private static transient Map<String, Object> objCollection = new HashMap<String, Object>();
	
	public static FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}
	
	public static Object getManagedBean(String beanName) {
		String ve = "#{requestScope." + beanName + "}";
		Object res = getFacesContext().getApplication().createValueBinding(ve).getValue(getFacesContext());
		return res;
	}
	
	public static void setManagedBean(String beanName, Object value) {
		String ve = "#{requestScope." + beanName + "}";
		getFacesContext().getApplication().createValueBinding(ve).setValue(getFacesContext(), value);
		return;
	}
	
	public static Object getManagedSessionBean(String beanName) {
		String ve = "#{sessionScope." + beanName + "}";
		Object res = getFacesContext().getApplication().createValueBinding(ve).getValue(getFacesContext());
		return res;
	}
	
	public static void setManagedSessionBean(String beanName, Object value) {
		String ve = "#{sessionScope." + beanName + "}";
		getFacesContext().getApplication().createValueBinding(ve).setValue(getFacesContext(), value);
		return;
	}
	
	public static Object getManagedApplicationBean(String beanName) {
		String ve = "#{applicationScope." + beanName + "}";
		Object res = getFacesContext().getApplication().createValueBinding(ve).getValue(getFacesContext());
		return res;
	}
	
	public static void setManagedApplicationBean(String beanName, Object value) {
		String ve = "#{applicationScope." + beanName + "}";
		getFacesContext().getApplication().createValueBinding(ve).setValue(getFacesContext(), value);
		return;
	}
	
	public static void addMessage(Severity severity, String msg) {
		MyApplicationContext.getFacesContext().addMessage(null, new FacesMessage(severity, msg, msg));
	}
	
	public static void putObject(String name, Object value) {
		objCollection.put(name, value);
		return;
	}
	
	public static Object getObject(String name) {
		Object obj = objCollection.get(name);
		return obj;
	}
	
}
